package com.jjc.android.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.jjc.android.*
import com.jjc.android.base.BaseActivity
import com.jjc.android.dialog.BottomWheelDialog
import com.jjc.android.entity.KeyValuePair
import com.jjc.android.util.GlobalActivityStack
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.view.onClick

class SettingActivity : BaseActivity() {

    override val layoutId: Int = R.layout.activity_setting
    private val logoutBtn: TextView by lazy { findViewById<TextView>(R.id.logoutBtn) }
    private val aboutUsBtn: TextView by lazy { findViewById<TextView>(R.id.aboutUsBtn) }
    private val pricyPolicyBtn: TextView by lazy { findViewById<TextView>(R.id.pricyPolicyBtn) }
    private val userPolicyBtn: TextView by lazy { findViewById<TextView>(R.id.userPolicyBtn) }
    private val versionBtn: LinearLayout by lazy { findViewById<LinearLayout>(R.id.versionBtn) }
    private val versionTxt: TextView by lazy { findViewById<TextView>(R.id.versionTxt) }
    private val modifyPwdBtn: TextView by lazy { findViewById<TextView>(R.id.modifyPwdBtn) }
    private val languageBtn:View by lazy { findViewById(R.id.languageBtn) }
    private val languageTxt:TextView by lazy { findViewById(R.id.languageTxt) }

    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.setting_txt))
        versionTxt.text = String.format("Va%s", BuildConfig.VERSION_NAME)
        modifyPwdBtn.onClick {
            startActivity(Intent(this,ModifyPasswordActivity::class.java))
        }
        pricyPolicyBtn.onClick {
            BrowserActivity.start(this, pageData = GlobalManager.getPrivacyPolicyContent(), title = getString(R.string.privacy_ag_txt))
        }
        userPolicyBtn.onClick {
            BrowserActivity.start(this, pageData = GlobalManager.getUserAgreementContent(), title = getString(R.string.user_ag_txt))
        }
        aboutUsBtn.onClick {
            BrowserActivity.start(this, pageData = GlobalManager.getAboutUsContent(), title = getString(R.string.about_us_txt))
        }
        logoutBtn.onClick {
            UserManager.logout{
                GlobalActivityStack.startAndFinishOthers(LoginActivity::class.java)
            }
        }
        languageTxt.text = when(MultiLanguageUtil.getInstance().languageId){
            MultiLanguageUtil.LANGUAGE_CHINESE->getString(R.string.language_chinese)
            MultiLanguageUtil.LANGUAGE_ENGLISH->getString(R.string.language_english)
            else->getString(R.string.language_system)
        }
        languageBtn.onClick {
            BottomWheelDialog<KeyValuePair>(this)
                .title(getString(R.string.language_txt))
                .contentList(listOf(
                    KeyValuePair(MultiLanguageUtil.LANGUAGE_SYSTEM.toString(),getString(R.string.language_system)),
                    KeyValuePair(MultiLanguageUtil.LANGUAGE_CHINESE.toString(),getString(R.string.language_chinese)),
                    KeyValuePair(MultiLanguageUtil.LANGUAGE_ENGLISH.toString(),getString(R.string.language_english))
                ))
                .choosePosition(when(MultiLanguageUtil.getInstance().languageId){
                    MultiLanguageUtil.LANGUAGE_CHINESE->1
                    MultiLanguageUtil.LANGUAGE_ENGLISH->2
                    else->0
                })
                .onItemClick { position, value ->
                    MultiLanguageUtil.getInstance().updateLanguage(value.key.toInt())
                    GlobalActivityStack.finishAll()
                    startActivity(Intent().apply {
                        setClass(this@SettingActivity,SplashActivity::class.java)
                        flags = Intent.FLAG_ACTIVITY_CLEAR_TASK and Intent.FLAG_ACTIVITY_NEW_TASK
                    })
                }
                .build()
                .show()
        }
    }
}